<template>
  
  <Invitation
  @click-like="clickLike" @get-follower="getFollower"
  @get-invitation-id="openComment"
  :DeleteInvitation="DeleteInvitation"
  v-if="loading" :invitation-data="invitationDatas"></Invitation>
  <uni-load-more v-if="!loading" :status="loading ? 'more' :'loading' " />
  <invitationComments
  ref="commentRef"
   :invitation_id="invitation_id" :comment_num="comment_num"
    @get-replyid="getReplyId"></invitationComments>
  <uni-popup
    ref="popup"
    type="bottom"
  >
    <view class="popup">
  <Input :move="false" :placeholder="placeholder" :focus="focus" @get-focus="lowsefocus" @get-msg="sendMsg"></Input>
    </view>
  </uni-popup>
</template>

<script lang="ts" setup>
import {http} from '@/utlis/http.ts'
import {ref} from 'vue'
import Invitation from '@/components/invitation.vue'
import invitationComments from '@/components/invitationComments.vue';
import Input from '@/components/inputcomment.vue';
//帖子相关
//删除帖子
const DeleteInvitation = (index)=>{
  invitationDatas.value.splice(index,1)
}
//点赞相关
const clickLike = (index)=>{
invitationDatas.value[index].like_type=!invitationDatas.value[index].like_type
uni.request({
  url: 'invitation/like/',
  data: {invitation_id:invitationDatas.value[index].id},
  header: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'X-Requested-With': 'XMLHttpRequest'
  },
  method: 'POST',
  sslVerify: true,
  success: ({ data, statusCode, header }) => {
    if(data.code == 200){
      invitationDatas.value[index].like=invitationDatas.value[index].like_type ?invitationDatas.value[index].like+1:invitationDatas.value[index].like-1
    }
    else{
      uni.showToast({
        title: data.errmsg,
        icon: 'error',
        mask: true
      })
    }
    
  },
  fail: (error) => {}
})
}
//改变关注
const getFollower =(index)=>{
  invitationDatas.value[index].is_follow=!invitationDatas.value[index].is_follow
}
const invitationId = ref('')
const comment_num = ref('')
const openComment=(id,num)=>{
  invitationId.value = id
  comment_num.value = num
  popup.value.open()
  console.log(id);
}



const Prop= defineProps<{
      invitation_id:number,
}>()

const invitationDatas = ref('')
const popup = ref()
const loading = ref(0)
const replyId = ref("")
const replyIndex = ref(0)
const getReplyId = (id,nickname,index)=>{
replyId.value = id
focus.value=true
replyIndex.value = index
placeholder.value=nickname
popup.value.open()
// console.log(id,nickname);

}
const page_data=ref({
  page_num:1,
  page_size:10,
  page_count:0
})
//回复评论
const commentRef = ref('')
const placeholder = ref('')
const focus = ref(false)
const ftext = ref(false)
const lowsefocus = ()=>{
  if(placeholder.value == '')ftext.value=false;
  else ftext.value=true;
  focus.value=false
  placeholder.value=''
}
const sendMsg = (data)=>{
  console.log(data);
  uni.request({
    url: 'invitation/create/comment/',
    data: {
      invitation_id:invitationId.value,
      comment:data.comment,
      url:data.url?data.url.split('https://www.damer.asia/')[1]:'',
      parent_id:ftext.value ? replyId.value:""
    },
    header: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'X-Requested-With': 'XMLHttpRequest'
    },
    method: 'POST',
    sslVerify: true,
    success: ({ data, statusCode, header }) => {
      console.log(data);
      if(data.code == 200){
          const temp=ftext.value ? replyIndex.value:-1
          console.log(temp);
          commentRef.value.addComment(data.data,temp)
      }
      else{
        uni.showToast({
          title: data.errmsg,
          icon: 'error',
          mask: true
        })
      }
      
    },
    fail: (error) => {}
    
  })
  focus.value=false
}
uni.request({
  url: `invitaion/detail/${Prop.invitation_id}/`,
  header: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'X-Requested-With': 'XMLHttpRequest'
  },
  method: 'GET',
  sslVerify: true,
  success: ({ data, statusCode, header }) => {
    console.log(data);
    if(data.code == 200){
      invitationDatas.value = [data.data.list]
      console.log(invitationDatas.value);
      comment_num.value = invitationDatas.value[0].comment_num
      loading.value=true
    }
    else{
      uni.showToast({
        title: data.errmsg,
        icon: 'error',
        mask: true
      })
    }
    
  },
  fail: (error) => {}
})

</script>

<style lang="scss" scoped>
.popup{
  width: 100%;
  background-color: #fff;
}
</style>